﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace BaMusaSoft.RPS.DataModel.Repositories
{
    /// <summary>
    /// Represent Schedules Repostiory. Instantiate via UnitOfWork object
    /// </summary>
    public sealed class SchedulesRepository : RepositoryBase<Schedule>
    {
        internal SchedulesRepository(RpsEntities context) : base(context)
        {
        }

        #region Overrides of BaseRepository<SchedulesInfo>

        /// <summary>
        /// Adds the specified entity to the repository.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public override void Add(Schedule entity)
        {
            Context.Schedules.Add(entity);
        }

        /// <summary>
        /// Deletes the specified entity from the repository.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public override void Delete(Schedule entity)
        {
            Context.Schedules.Remove(entity);
        }

        /// <summary>
        /// Gets the entity by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public override Schedule GetById(object id)
        {
            if (id == null || id.ToString() == string.Empty) throw new ArgumentNullException("id");
            string scheduleId = (string) id;
            return Context.Schedules
                    .Include(s => s.Contract.Customer)
                    .Include(s => s.Contract.Property)
                    .Include(s => s.Signer)
                    .Include(s => s.ScheduleDetails)
                    .Where(x => x.ScheduleId == scheduleId).Single();
        }

        /// <summary>
        /// Query repository by specified filter.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public override IQueryable<Schedule> Query(Expression<Func<Schedule, bool>> filter)
        {
            return
                Context.Schedules
                    .Include(s => s.Contract.Customer)
                    .Include(s => s.Contract.Property)
                    .Include(s => s.Signer)
                    .Include(s => s.ScheduleDetails)
                    .Where(filter);
        }

        /// <summary>
        /// Gets all entities in the repository.
        /// </summary>
        /// <returns></returns>
        public override IList<Schedule> GetAll()
        {
            return
                Context.Schedules
                    .Include(s => s.Contract.Customer)
                    .Include(s => s.Contract.Property)
                    .Include(s => s.Signer)
                    .Include(s => s.ScheduleDetails)
                    .ToList();
        }

        public override void Refresh()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region "Repository Specifiec Members"
        public string MaxSchedule
        {
            get
            {
                return
                    (from schedule in Context.Schedules select schedule.ScheduleId).Max();
            }
        }
        public bool IsScheduledContract(Contract contract)
        {
            return Context.Schedules.Any(s => s.Contract.ContractNo == contract.ContractNo);
        }
        #endregion

    }
}